package cn.cmxds.store.view.Impl.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.sunfusheng.marqueeview.MarqueeView;
import com.zhhr.custom.ZToast;

import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.cmxds.store.R;
import cn.cmxds.store.adapter.HomeCatgoryAdapter;
import cn.cmxds.store.adapter.SortGridViewAdapter;
import cn.cmxds.store.adapter.decoration.CardViewtemDecortion;
import cn.cmxds.store.application.CustomApplcation;
import cn.cmxds.store.bean.Banner;
import cn.cmxds.store.bean.Campaign;
import cn.cmxds.store.bean.HomeCampaign;
import cn.cmxds.store.config.Contracts;
import cn.cmxds.store.presenter.IHomePresenter;
import cn.cmxds.store.presenter.Impl.HomePresenter;
import cn.cmxds.store.util.ToastFactory;
import cn.cmxds.store.view.IHomeView;
import cn.cmxds.store.view.Impl.activity.GoodsResultActivity;
import cn.cmxds.store.view.Impl.activity.GoodsTypeActivity;
import cn.cmxds.store.view.Impl.activity.WebViewActivity;
import cn.cmxds.store.widget.AlertDialog;
import cn.cmxds.store.widget.MyToolBar;

public class HomeViewFragment extends BaseFragment implements IHomeView {


    @BindView(R.id.frag_home_gv_sort)
    GridView gridViewSort;
    @BindView(R.id.slider)
    SliderLayout mSliderLayout;
    @BindView(R.id.frag_home_ln_top)
    LinearLayout linearLayoutTop;
    @BindView(R.id.frag_home_marqueeView)
    MarqueeView marqueeViewTop;
    @BindView(R.id.toolbar)
    MyToolBar toolbar;
    @BindView(R.id.toolbar_searchview)
    EditText mFragHomeEtSearch;
    Unbinder unbinder;
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    Unbinder unbinder1;
    private TextSliderView textSliderView;
    private IHomePresenter prenseter;
    private HomeCatgoryAdapter mAdatper;

    @Override
    protected void lazyLoad() {

    }

    public HomeCatgoryAdapter getmAdatper() {
                 return mAdatper;
          }

    public TextSliderView getTextSliderView() {
        return textSliderView;
    }

    /**
     * @param inflater  布局填充器
     * @param container ViewGroup
     * @param b         标记
     * @return
     */
    @Override
    public View initLayout(LayoutInflater inflater, ViewGroup container, boolean b) {
        View rootView = inflater.inflate(R.layout.fragment_home, null);
        ButterKnife.bind(this, rootView);
        return rootView;
    }


    /**
     * 初始化数据
     *
     * @param savedInstanceState
     */
    @Override
    protected void initData(@Nullable Bundle savedInstanceState) {
        new HomePresenter(this);
        prenseter.initData();
    }


    /**
     * 弹出消息
     *
     * @param msg
     */
    @Override
    public void showMsg(String msg) {
        ZToast.init(Color.parseColor("#eb4f38"),Color.parseColor("#ffffff"),false,0,55);
        ZToast.makeText(getActivity(),msg,1000).show();
//        new AlertDialog(getActivity()).builder() .setMsg(msg).setTitle("提示")
//                .setNegativeButton("取消", new View.OnClickListener() {
//            @Override public void onClick(View v) { } })
//                .setPositiveButton("确定",new View.OnClickListener() {
//                    @Override public void onClick(View v) { } })
//                .show();
//        ToastFactory.getToast(getContext(), msg).show();

    }

    /**
     * 弹出加载对话框
     *
     * @param title 标题
     * @param msg   显示的内容
     * @param flag  是否可以取消
     */
    @Override
    public void showLoadingDialog(String title, String msg, boolean flag) {

    }

    /**
     * 取消加载对话框
     */
    @Override
    public void canelLoadingDialog() {

    }

    /**
     * 取得首页按钮
     *
     * @return
     */
    @Override
    public GridView getGridViewSort() {
        return gridViewSort;
    }



    /**
     * 取得滚动资讯
     *
     * @return
     */
    @Override
    public MarqueeView getMarqueeViewTop() {
        return marqueeViewTop;
    }

    /**
     * 跳转
     *
     * @param type  类型 1-9
     * @param datas
     */
    @Override
    public void jumpActivity(int type, String datas) {
        Intent intent = null;
        switch (type) {
            case 0:
                intent = new Intent(getContext(), WebViewActivity.class);
                intent.putExtra("URL", Contracts.TIANMAO_URL);
                break;
            case 1:
                intent = new Intent(getContext(), WebViewActivity.class);
                intent.putExtra("URL", Contracts.JUHUASUAN_URL);
                break;
            case 2:
                intent = new Intent(getContext(), WebViewActivity.class);
                intent.putExtra("URL", Contracts.TIANMAO_GUOJI_URL);
                break;
            case 3:
                intent = new Intent(getContext(), WebViewActivity.class);
                intent.putExtra("URL", Contracts.WAIMAI);
                break;
            case 4:
                intent = new Intent(getContext(), WebViewActivity.class);
                intent.putExtra("URL", Contracts.TIANMAO_SUPERMARKET);
                break;
            case 5:
                intent = new Intent(getContext(), WebViewActivity.class);
                intent.putExtra("URL", Contracts.CHONGZHI_CENTER);
                break;
            case 6:
                intent = new Intent(getContext(), WebViewActivity.class);
                intent.putExtra("URL", Contracts.ALI_TRAVEL);
                break;
            case 7:
                intent = new Intent(getContext(), WebViewActivity.class);
                intent.putExtra("URL", Contracts.LING_MONEY);

                break;
            case 8:
                intent = new Intent(getContext(), WebViewActivity.class);
                intent.putExtra("URL", Contracts.DAO_JIA);
                break;
            case 9:
                intent = new Intent(getContext(), GoodsTypeActivity.class);

                break;
            case 10://搜索框
                intent = new Intent(getContext(), GoodsResultActivity.class);
                //将数据传递到下一个activity
                intent.putExtra("datas", datas);
            case 11://资讯框
                intent =new Intent(getContext(),WebViewActivity.class);
                intent.putExtra("url",datas);
        }
        startActivity(intent);
    }

    /**
     * 初始化轮播动画
     *
     * @param mBanner
     */
    public void initSlider(List<Banner> mBanner) {

        for (final Banner banner : mBanner) {
            textSliderView = new TextSliderView(CustomApplcation.getInstance().context);
            textSliderView.image(banner.getImgUrl());
            textSliderView.description(banner.getName());
            textSliderView.setScaleType(BaseSliderView.ScaleType.Fit);
            textSliderView.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
                @Override
                public void onSliderClick(BaseSliderView slider) {
                    showMsg(banner.getImgUrl());
                }
            });
            mSliderLayout.addSlider(textSliderView);
        }
        mSliderLayout.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        mSliderLayout.setCustomAnimation(new DescriptionAnimation());
        mSliderLayout.setPresetTransformer(SliderLayout.Transformer.RotateUp);
        mSliderLayout.setDuration(3000);
    }

    public void initRecyclerView(List<HomeCampaign> homeCampaigns) {

        mAdatper = new HomeCatgoryAdapter(homeCampaigns, CustomApplcation.getInstance().context);

        mAdatper.setOnCampaignClickListener(new HomeCatgoryAdapter.OnCampaignClickListener() {
            @Override
            public void onClick(View view, Campaign campaign) {

                    showMsg(campaign.getId().toString());

            }
        });

        mRecyclerView.setAdapter(mAdatper);

        mRecyclerView.addItemDecoration(new CardViewtemDecortion());

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));

    }

    @Override
    public void initGridView(Integer[] sortImgs, String[] sortStrs, int layout) {

        gridViewSort.setAdapter(new SortGridViewAdapter(CustomApplcation.getInstance().context, sortImgs, sortStrs, layout));

    }

    @Override
    public void initViewTop(List<String> info) {
        marqueeViewTop.startWithList(info);
    }

    @Override
    public EditText getmFragHomeEtSearch() {

        return mFragHomeEtSearch;
    }


    @Override
    public void setPresenter(IHomePresenter presenter) {
        this.prenseter = presenter;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mSliderLayout.stopAutoCycle();
    }


}
